﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;

namespace GRWL.BZSC.UI
{
    public partial class EditSupplierFrm : BaseFrm
    {

        /// <summary>
        /// 供应商记录编辑调用设定为 true, 客户记录编辑调用设定为 false
        /// </summary>
        public bool IsSupplierRec { get; set; }

        /// <summary>
        ///新增时当前所在分类
        /// </summary>
        //public SelectItem CurrentCategory { get; set; }
        public int Type { get; set; }

        /// <summary>
        /// 传入编码规则
        /// </summary>
        //public CodeRuleFrm.CodeRule codeRule { get; set; }

        /// <summary>
        /// 编辑模式时传入id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 新增调用设定为 true, 编辑调用设定为 false
        /// </summary>
        public bool EditMode { get; set; }

        //public bool CallDone { get; set; }
        private TB_Trader Trader { get; set; }

        //public event GRWL.BZSC.Common.EventCommon.UpdateDataGridViewDlgt UpdateDataGridViewEvent;

        public EditSupplierFrm()
        {
            InitializeComponent();

            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();            
        }

        private void EditSupplierFrm_Load(object sender, EventArgs e)
        {
            //初始化
            if (IsSupplierRec)
            {
                this.Text = "供应商资料";
            }
            else
            {
                this.Text = "客户资料";
            }
            isSupplierCheckBox.Checked = IsSupplierRec;
            isClientCheckBox.Checked = !IsSupplierRec;
            isSupplierCheckBox.Enabled = !IsSupplierRec;
            isClientCheckBox.Enabled = IsSupplierRec;

            //新增记录时此值为空
            //if (Trader == null)
            if (!EditMode)
            {
                Trader = new TB_Trader();
                typeUTextBox.Text = Type.ToString();
                //指定新记录中的默认分类
                Trader.type = Type;
                return;
            }

            Trader = this.BaseDataProxy.GetTrader(Id);
            //编辑记录的内容绑定到对应控件
            codeUTextBox.Text = Trader.code;
            nameUTextBox.Text = Trader.name;
            linkmanUTextBox.Text = Trader.linkman;
            telUTextBox.Text = Trader.tel;
            faxUTextBox.Text = Trader.fax;
            addressUTextBox.Text = Trader.address;
            isClientCheckBox.Checked = Trader.isClient;
            isSupplierCheckBox.Checked = Trader.isSupplier;
            taxRateUTextBox.Text = Trader.taxRate.ToString();
            typeUTextBox.Text = Trader.type.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Trader.code = codeUTextBox.Text;
            Trader.name = nameUTextBox.Text;
            Trader.linkman = linkmanUTextBox.Text;
            Trader.tel = telUTextBox.Text;
            Trader.fax = faxUTextBox.Text;
            Trader.address = addressUTextBox.Text;
            Trader.isClient = isClientCheckBox.Checked;
            Trader.isSupplier = isSupplierCheckBox.Checked;
            Trader.taxRate = Common.StringToDecimal(taxRateUTextBox.Text);
            Trader.type = Common.StringToInt(typeUTextBox.Text);

            GRWL.BZSC.UI.Common.OperateType opType = Common.OperateType.Add;
            try
            {
                if (!EditMode) //String.IsNullOrEmpty(codeUTextBox.Text)
                {
                    int g = BaseDataProxy.AddTrader(Trader);
                    if (g > 0)
                    {
                        Trader.id = g;

                        MessageBox.Show("已成功保存记录.");
                    }
                }
                else
                {
                    opType = Common.OperateType.Update;
                    if (BaseDataProxy.UpdateTrader(Trader))
                    {
                        MessageBox.Show("已成功保存记录.");
                    };
                }
            }
            catch (InvalidCastException eMsg)
            {
                MessageBox.Show(eMsg.Message.ToString());
            }

            CommonEventArg<TB_Trader> tdr = new CommonEventArg<TB_Trader>(Trader, opType);
            if (IsSupplierRec)            
                GRWL.BZSC.Common.EventCommon.OnSupplierUdDGVEvnt(sender, tdr);            
            else
                GRWL.BZSC.Common.EventCommon.OnClientUdDGVEvnt(sender, tdr);
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
